package main

import (
	"log"
	"mytest/week9/pkg/bufio"
	"mytest/week9/protocol"
	"net"
)

func main() {
	listen, err := net.Listen("tcp", "127.0.0.1:10000")
	if err != nil {
		log.Fatalf("listen error: %v\n", err)
	}
	for {
		conn, err := listen.Accept()
		if err != nil {
			log.Printf("accept error: %v\n", err)
			continue
		}
		// 开始goroutine监听连接
		go handleConn(conn)
	}
}

func handleConn(conn net.Conn) {
	defer conn.Close()
	// 读写缓冲区
	rr := bufio.NewReader(conn)
	pro := &protocol.Proto{}
	go pro.ReadTCP(rr)

	//wr := bufio.NewWriter(conn)
	//fmt.Printf("%+v\n", pro)
	//fmt.Println(string(pro.Body))
	//message := "hello " + string(pro.Body)
	//bwr := bytes.NewWriterSize(len(message))
	//bwr.Write([]byte(message))
	//_ = pro.WriteTo(bwr)
	//_, _ = wr.Write(bwr.Buffer())
}
